﻿using PLCS.App.LogManager.Service;
using PLCS.App.Shared.Extensions;
using PLCS.App.Shared.ViewModels;
using PLCS.Application.Contracts.InterfaceApp.Dtos;
using PLCS.Application.Contracts.Params;
using PLCS.Client.Service.LogManager;
using Prism.Commands;
using System.Collections.ObjectModel;
using System.Threading.Tasks;

namespace PLCS.App.LogManager.ViewModels;

public class InterfaceLogViewModel : NavigationPagedCurdViewModel<InterfaceLogDto, InterfaceLogDto, InterfaceLogService>
{
    private string _startTime = string.Empty, _endTime = string.Empty;
    private bool _isRightDrawerOpen;
    private ObservableCollection<PropertyNodeItem> _itemList;
    private string _requestSide, _responseSide;

    public string StartTime
    {
        get { return _startTime; }
        set { _startTime = value; RaisePropertyChanged(); }
    }

    public string EndTime
    {
        get { return _endTime; }
        set { _endTime = value; RaisePropertyChanged(); }
    }

    public string RequestSide
    {
        get { return _requestSide; }
        set { _requestSide = value; RaisePropertyChanged(); }
    }

    public string ResponseSide
    {
        get { return _responseSide; }
        set { _responseSide = value; RaisePropertyChanged(); }
    }

    public DelegateCommand<InterfaceLogDto> OpenResponseCommand { get; set; }
    public DelegateCommand<InterfaceLogDto> OpenRequestCommand { get; set; }

    public bool IsRightDrawerOpen
    {
        get { return _isRightDrawerOpen; }
        set { _isRightDrawerOpen = value; RaisePropertyChanged(); }
    }

    public ObservableCollection<PropertyNodeItem> ItemList
    {
        get { return _itemList; }
        set { _itemList = value; RaisePropertyChanged(nameof(ItemList)); }
    }

    public override async Task GetPageData(int pageIndex)
    {
        try
        {
            UpdateLoading(true);
            var res = await _service.GetListBySearchAsync((pageIndex - 1) * PageSize, PageSize, StartTime, EndTime, RequestSide, ResponseSide);
            TotalCount = (int)res.TotalCount;
            SetPageCount();
            DataGridList = res.Items.ToObservable();
        }
        finally
        {
            UpdateLoading(false);
        }
    }

    public InterfaceLogViewModel(InterfaceLogService service) : base(service)
    {
        OpenResponseCommand = new DelegateCommand<InterfaceLogDto>(OpenResponse);
        OpenRequestCommand = new DelegateCommand<InterfaceLogDto>(OpenRequest);
    }

    private void OpenRequest(InterfaceLogDto obj)
    {
        IsRightDrawerOpen = true;
        IDataConvert dataConvert = DataConvertFactory.CreateInstance(obj.DataType);
        ItemList = (dataConvert.GetTreeModels(obj.RequestParam)).ToObservable();
    }

    private void OpenResponse(InterfaceLogDto obj)
    {
        IsRightDrawerOpen = true;
        IDataConvert dataConvert = DataConvertFactory.CreateInstance(obj.DataType);
        ItemList = (dataConvert.GetTreeModels(obj.ResponseData)).ToObservable();
    }
}